package com.slz3.business.module.goodsManagement.gooGoods.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooBrand.domain.GooBrand;
import com.slz3.business.module.goodsManagement.gooGoods.domain.GooGoods;
import com.slz3.business.module.goodsManagement.gooGoods.domain.GooGoodsFactory;
import com.slz3.business.module.goodsManagement.gooGoods.steward.GooGoodsSpecification;
import com.slz3.business.module.goodsManagement.gooGoods.steward.GooGoodsSteward;
import com.slz3.business.module.goodsManagement.gooTypeLevel1.domain.GooTypeLevel1;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.domain.GooTypeLevel2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GooGoodsServiceImpl implements GooGoodsService {

    @Override
    public GooGoods save(GooGoods gooGoods) {
        // redundancy
        GooGoodsSteward.redundancy(gooGoods);
        //
        return Repositorys.gooGoods.save(GooGoodsFactory.initBaseFields(gooGoods));
    }

    @Override
    public Page<GooGoods> page(MultiValueMap<String, String> params) {
        Page<GooGoods> gooGoodsPage = Repositorys.gooGoods.findAll(
                GooGoodsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        // join
        Map<UUID, GooBrand> joinBrandType = Repositorys.gooBrand.findAllById(
                gooGoodsPage.getContent().stream().map(GooGoods::getSerGoodsBarndId).collect(Collectors.toList())
        ).stream().collect(Collectors.toMap(GooBrand::getUuid, Function.identity()));
        gooGoodsPage.getContent().stream().forEach(item -> item.setGooBrand(joinBrandType.get(item.getSerGoodsBarndId())));
        // join
        System.err.println(gooGoodsPage.getContent().stream().map(GooGoods::getSerGoodsT1).collect(Collectors.toList()));
        Map<UUID, GooTypeLevel1> joinLevel1 = Repositorys.gooTypeLevel1.findAllById(
                gooGoodsPage.getContent().stream().map(GooGoods::getSerGoodsT1).collect(Collectors.toList())
        ).stream().collect(Collectors.toMap(GooTypeLevel1::getUuid, Function.identity()));
        System.err.println(joinLevel1.size());
        gooGoodsPage.getContent().stream().forEach(item -> item.setGooTypeLevel1(joinLevel1.get(item.getSerGoodsT1())));
        // join
        Map<UUID, GooTypeLevel2> joinLevel2 = Repositorys.gooTypeLevel2.findAllById(
                gooGoodsPage.getContent().stream().map(GooGoods::getSerGoodsT2).collect(Collectors.toList())
        ).stream().collect(Collectors.toMap(GooTypeLevel2::getUuid, Function.identity()));
        gooGoodsPage.getContent().stream().forEach(item -> item.setGooTypeLevel2(joinLevel2.get(item.getSerGoodsT2())));
        //redis库存
        gooGoodsPage.getContent().stream().forEach(item -> item.setSerGoodsRedisStock(Jedis.db0.get(RK.WMS.simple(item.getUuid().toString()))));
        return gooGoodsPage;
    }

    @Override
    public GooGoods modify(GooGoods gooGoodsNew) {
        // redundancy
        GooGoodsSteward.redundancy(gooGoodsNew);
        //
        return Repositorys.gooGoods.saveAndFlush(GooGoodsFactory.initBaseFields(gooGoodsNew));
    }

    @Override
    public Optional<GooGoods> single(UUID uuid) {
        return Repositorys.gooGoods.findById(uuid);
    }

    @Override
    public List<GooGoods> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooGoods.findAllById(uuids);
    }

    @Override
    public List<GooGoods> delete(List<GooGoods> gooGoodsList) {
        gooGoodsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooGoodsFactory.initBaseFields(item);
        });
        return Services.gooGoodsTransactional.batchModify(gooGoodsList);
    }

}
